<!-- @format -->

<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="UTF-8" />
		<meta name="viewport" content="width=device-width, initial-scale=1.0" />
		<title>Document</title>
	</head>

	<body>
		<script>
			// 猜数字游戏，设定次数，最多猜8次
			let randomNum = Math.floor(Math.random() * 100);

			let chance = 8;
			while (chance > 0) {
				let guess = +prompt("请猜一个数：");
				if (guess > randomNum) {
					alert("猜大了");
					chance--;
				} else if (guess < randomNum) {
					alert("猜小了");
					chance--;
				} else {
					alert("恭喜你猜对了");
					break;
				}
			}

			if (chance === 0) {
				alert("机会已经耗尽!");
			}
		</script>
	</body>
</html>
